package com.llkj.safeearth.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import java.util.HashMap;
import java.util.Set;

/**
 * sharedPreference信息保存类
 * 
 */
public class SpUtil {
	private SharedPreferences sp = null;
	private static SpUtil spUtil = null;
	private static final String SP_USERINFO_FILE_NAME = "sp_userinfo";
	public static final String USER_ID = "user_id";//用户id

	public static SpUtil init(Context context) {
		spUtil = new SpUtil(context, SP_USERINFO_FILE_NAME, Context.MODE_PRIVATE);
		return spUtil;
	}

	private SpUtil(Context context, String sp_name, int mode) {
		super();
		sp = context.getSharedPreferences(sp_name, mode);
	}

	/**
	 * 保存String
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean put(String key, String value) {
		Editor edit = sp.edit();
		edit.putString(key, value);
		return edit.commit();
	}

	/**
	 * 保存boolean
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean put(String key, boolean value) {
		Editor edit = sp.edit();
		edit.putBoolean(key, value);
		return edit.commit();
	}

	/**
	 * 保存float
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean put(String key, float value) {
		Editor edit = sp.edit();
		edit.putFloat(key, value);
		return edit.commit();
	}

	/**
	 * 保存long
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean put(String key, long value) {
		Editor edit = sp.edit();
		edit.putLong(key, value);
		return edit.commit();
	}

	/**
	 * 保存int
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean put(String key, int value) {
		Editor edit = sp.edit();
		edit.putInt(key, value);
		return edit.commit();
	}

	public int getInt(String key) {
		return sp.getInt(key, 0);
	}

	public boolean getBoolean(String key) {
		return sp.getBoolean(key, false);
	}

	public String getString(String key) {
		return sp.getString(key, "");
	}

	public float getFloat(String key) {
		return sp.getFloat(key, 0.0f);
	}

	public long getLong(String key) {
		return sp.getLong(key, 0);
	}

	/**
	 * 批量保存
	 * 
	 * @param map
	 */
	public void put(HashMap<String, Object> map) {
		if (map != null && map.size() > 0) {
			Editor edit = sp.edit();
			Set<String> set = map.keySet();
			for (String key : set) {
				Object value = map.get(key);
				if (value instanceof String) {
					edit.putString(key, (String) value);
				} else if (value instanceof Integer) {
					edit.putInt(key, (Integer) value);
				} else if (value instanceof Boolean) {
					edit.putBoolean(key, (Boolean) value);
				} else if (value instanceof Float) {
					edit.putFloat(key, (Float) value);
				} else if (value instanceof Long) {
					edit.putLong(key, (Long) value);
				} else {
					Log.e("Log","保存 >>跳过了一些不属于 string,int,boolean,float,long 类型的值.不支持的类型: ");
					// + value.getClass().getSimpleName()
				}
			}
			boolean b = edit.commit();
		}
	}

	public boolean contains(String key) {
		return sp.contains(key);
	}

	public void clear() {
		sp.edit().clear().commit();
	}
}
